//
//  SetPasswordViewModel.m
//  GLGW_Shoping
//
//  Created by yang on 2018/11/17.
//  Copyright © 2018年 yang. All rights reserved.
//

#import "SetPasswordViewModel.h"
#import "SetPasswordTableViewCell.h"
#import "LoginActionTableViewCell.h"
#import "RegistereFootView.h"
#import "RegistereModel.h"
#import "LoginModel.h"

@interface SetPasswordViewModel ()<UITableViewDataSource,UITableViewDelegate,SetPassWordDelegate>
{
    GLGWButton * comintBut;
    NSMutableDictionary * reginsDic;
    //
    NSString * passworderStr;
}
@end

@implementation SetPasswordViewModel

-(instancetype)initWithViewToViewModel:(UITableView *)tableView theSuperView:(UIView *)superView{
    if (self = [super initWithViewToViewModel:tableView theSuperView:superView]) {
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.mj_footer.hidden = YES;
        tableView.mj_header.hidden = YES;
        tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, kHeightProportion_In_iPhoneTriploid(65))];
        tableView.tableFooterView = [self createFootView:tableView];
        [superView addSubview:tableView];
        //
        self.btnClickSignal = [RACSubject subject];
        @weakify(self)
        [self.btnClickSignal subscribeNext:^(id  _Nullable x) {
            @strongify(self)
            self->reginsDic = (NSMutableDictionary *)x;
        }];
    }
    return self;
}

-(UIView *)createFootView:(UITableView *)t_view{
    RegistereFootView * footView = [[RegistereFootView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, t_view.height - kHeightProportion_In_iPhoneTriploid(225))];
    return footView;
}

#pragma mark -- UITableViewDataSource,UITableViewDelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 2;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        return kHeightProportion_In_iPhoneTriploid(62);
    }else{
        return kHeightProportion_In_iPhoneTriploid(94);
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BaseTableViewCell * cell;
    switch (indexPath.row) {
        case 0:{
            SetPasswordTableViewCell * cell_pass = [SetPasswordTableViewCell cellWithTableView:tableView];
            cell_pass.delegate =self;
            cell = cell_pass;
        }
            break;
        case 1:{
            LoginActionTableViewCell * cell_Action = [LoginActionTableViewCell cellWithTableView:tableView];
            comintBut  = cell_Action.actionBut;
            [comintBut addTarget:self action:@selector(setPasswordOk) forControlEvents:UIControlEventTouchUpInside];
            cell = cell_Action;
        }
            break;
            
        default:
            break;
    }
    return cell;
}


-(void)setPasswordOk{
    [reginsDic setObject:koString(passworderStr) forKey:@"password"];
    NSString * apiStr;
    NSString * tosStr;
    switch (_set_pushType) {
        case RegisterePushType_RetrievePassword:{
            apiStr = kRetrievePassword;
            tosStr = @"找回密码成功！";
            [reginsDic removeObjectForKey:@"type"];
            NSString * phonrStr = [reginsDic objectForKey:@"mobilePhone"];
            [reginsDic removeObjectForKey:@"mobilePhone"];
            [reginsDic setObject:phonrStr forKey:@"username"];
        }
            break;
        case RegisterePushType_BindingPhone:{
            apiStr = kThridBindRegistor;
            tosStr = @"绑定手机成功！";
        }
            break;
        case RegisterePushType_Registere:{
            apiStr = kRegister;
            tosStr = @"注册成功！";
        }
            break;
            
            
        default:
            break;
    }
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,apiStr] lodingView:nil lodingTextString:@"" isLoding:YES parameters:reginsDic success:^(id  _Nullable response) {
        RegistereModel * regis_model = [RegistereModel yy_modelWithJSON:response];
        if (regis_model.code == 0) {
            [GLGWProgressHUD showNotice:[NSString stringWithFormat:@"恭喜您，%@",tosStr] view:nil];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                if (self.set_pushType != RegisterePushType_BindingPhone) {
                    [self loginRequest:self.set_pushType];
                }
            });
        }
    } failure:^(NSError * _Nullable error) {

    }];

}

-(void)loginRequest:(RegisterePushType)t_type{
    NSDictionary * preDic;
    if (t_type == RegisterePushType_Registere) {
        preDic = @{@"username":[reginsDic objectForKey:@"mobilePhone"],@"password":[reginsDic objectForKey:@"password"]};
    }else{
        preDic = @{@"username":[reginsDic objectForKey:@"username"],@"password":[reginsDic objectForKey:@"password"]};
    }
    [GLGWRequestTool POST:[NSString stringWithFormat:@"%@%@",kApiPrefix,kLogin] lodingView:nil lodingTextString:@"" isLoding:YES parameters:preDic success:^(id  _Nullable response) {
        LoginModel * loginModel = [LoginModel yy_modelWithJSON:response];
        if (loginModel.code == 0) {
            NSData * userData = [NSKeyedArchiver archivedDataWithRootObject:loginModel];
            self.userInforData = userData;
            self.userId = loginModel.data.userId;
            [self.delegate loginSuccess];
        }
    } failure:^(NSError * _Nullable error) {
        
    }];
}

#pragma mark -- SetPassWordDelegate
-(void)inputPassword:(NSString *)p_word{
    passworderStr = p_word;
    if (p_word.length >0) {
        comintBut.backgroundColor = kColor_Rule_Yellow;
        comintBut.enabled = YES;
    }else{
        comintBut.backgroundColor = [UIColor colorWithHexString:@"#CCCCCC"];
        comintBut.enabled = NO;
    }
}

@end
